package org.meandslava.calc.arithmetic;

import android.text.Editable;

public class FixedNumber extends Number {
	private StringBuilder builder = new StringBuilder();
	
	public FixedNumber(int number){
		builder.append(number);
	}
	
	public FixedNumber(double number){
		builder.append(format.format(number));
	}

	public FixedNumber(String tag) {
		if(tag.equals(".")){
			builder.append("0.");
		}else{
			builder.append(tag);
		}
	}

	@Override
	public double getValue() {
		return Double.parseDouble(builder.toString());
	}

	@Override
	public boolean process(String categoryTag, String action) {
		if(categoryTag.equals(CATEGORY_FIXED_NUMBER)){
			if(action.equals(".")){
				if(!hasPoint()){
					builder.append(".");
				}
			}else{
				builder.append(action);
			}
			return true;
		}
		return false;
	}
	
	@Override
	public boolean back() {
		if(builder.length() == 0){
			return true;
		}else{
			if(builder.deleteCharAt(builder.length()-1).length() == 0){
				return true;
			}
		}
		return false;
	}

	@Override
	public void write(Editable editable) {
		editable.append(builder.toString());
	}
	
	private boolean hasPoint() {
		return builder.toString().contains(".");
	}
}
